const fs = require("fs")
const bodyParser = require('body-parser')
const express = require('express')
const {register} = require('./personal_modules/register.js')
const {login} = require('./personal_modules/login.js')
const session = require('express-session')
const getUser = require('./personal_modules/getUser')
const upload = require('./personal_modules/upload')
const goodsup = require('./personal_modules/goodsup')
const getGoods = require('./personal_modules/getGoods')
const jsonpSearch = require('./personal_modules/jsonpSearch')
const sortGoodsPrice = require('./personal_modules/sortGoodsPrice')
const sortGoodsPopularity = require('./personal_modules/sortGoodsPopularity')
const goodsClassify = require('./personal_modules/goodsClassify')


const multer = require('multer')
const upload_path = multer({dest:'./user_upload'})

const setUser = require('./personal_modules/setUser')

const app = express()

app.all("*",(req,res,next)=>{
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Methods', 'get,post')
    res.header('Access-Control-Allow-Headers', 'Content-Type')
    next();
})

app.use(upload_path.any())

app.use(bodyParser.urlencoded({extended:false}))

app.use(express.static("./static_files"))
app.use(express.static('./user_upload'))

app.use(session({
    secret:'我就是加密内容',
    resave:false,
    saveUninitialized:true,
    cookie:{maxAge:1000 * 10 * 100}
}))

app.get('/login',login('成功调用login函数'))

app.post('/register',register('成功调用register函数'))

app.get('/getUser',getUser())

app.post('/upload',upload())

app.post('/setUser',setUser())

// app.get('/getGoods', (req,res,next)=>{
//     fs.readFile("./data/goods.json", "utf-8", (err,data)=>{
//         if(err){
//             res.send(JSON.stringify({
//                 code:0,
//                 msg:"数据请求失败",
//                 data:null
//             }))
//         }else{
//             res.send(JSON.stringify({
//                 code:1,
//                 msg:"数据请求成功",
//                 data:JSON.parse(data)
//             }))
//         }
//     })
// })

app.get("/getGoods_", (req,res,next)=>{
    fs.readFile("./data/goods_.json", "utf-8", (err,data)=>{
        if(err){
            res.send(JSON.stringify({
                code:0,
                msg:"数据请求失败",
                data:null
            }))
        }else{
            // console.log(data);
            res.send(JSON.stringify({
                code:1,
                msg:"数据请求成功",
                data:JSON.parse(data)
                
            }))
        }
    })
})

app.get('/getGoods', getGoods())

app.get('/sortGoodsPrice', sortGoodsPrice())

app.get('/sortGoodsPopularity',  sortGoodsPopularity())

app.get('/goodsClassify', goodsClassify())

app.get("/getDetail_", (req,res,next)=>{
    const id = req.query.id;
    fs.readFile("./data/goods_.json", "utf-8", (err,data)=>{
        const d = JSON.parse(data);
        let i;
        const flag = d.some((val,idx)=>{
            i = idx;
            return val.id.toString() === id;
        })
        if(flag){
            res.send(JSON.stringify({
                code:1,
                msg:"获取成功",
                data:d[i]
            }));
        }else{
            res.send(JSON.stringify({
                code:0,
                msg:"没有对应商品，请检查id是否正确",
                data:null
            }));
        }
    })
})

app.get('/goodsup',goodsup()) // 配合test.html使用，用来向数据库传入数据


app.get('/jsonpSearch',jsonpSearch())

app.listen(65535,()=>{
    console.log("Server is running...")
});